﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Forget.HRBLL.Model;
using System.Text.RegularExpressions;
using System.Data.SqlClient;
using System.Data;
using Forget.Data;
using Forget.HRBLL.View;

namespace Forget.HRBLL.Logical
{
    public class UserLimitLogical
    {
        /// <summary>
        /// 获得当前登录用户权限
        /// </summary>
        /// <param name="numberID"></param>
        /// <param name="enID"></param>
        /// <param name="lAttr"></param>
        /// <returns></returns>
        public static bool GetUserLimit(string numberID, string enID,LimitAttribute lAttr)
        {
            if (!string.IsNullOrEmpty(numberID) && !string.IsNullOrEmpty(enID))
            {
                List<LimitInfo> limits = LimitInfo.UserGetLimitInfos(numberID, enID);
                if (limits!=null && limits.Count > 0)
                {
                    if ((limits[0].Attributes & (long)lAttr) != 0)
                        return true;
                }
            }
            return false;
        }

        /// <summary>
        /// 添加权限
        /// </summary>
        /// <param name="eID"></param>
        /// <param name="name"></param>
        /// <param name="used"></param>
        /// <returns></returns>
        public static bool AddLimit(string eID, string name,bool used)
        {
            if (Regex.IsMatch(eID, "^[a-zA-Z]{1,20}$"))
            {
                if (Limit.AddLimit(new Limit() { EnID = eID, LimitName = name, Used = used }))
                    return true;                
            }
            else
                throw new ArgumentException("输入的标识不符合要求");
            return false;
        }

        /// <summary>
        /// 获取指定用户的权限集合
        /// </summary>
        /// <param name="numberID"></param>
        /// <returns></returns>
        public static List<LimitView> GetUserOperateLimit(string numberID)
        {
            return LimitView.GetUserLimits(new LimitView() { NumberID = numberID });
        }

        public static bool ModifyUserOperateLimit(List<LimitView> views)
        {
            if (views != null)
            {
                return LimitView.ModifyUserLimit(views);   
            }
            return true;
        }
    }
}


namespace Forget.HRBLL.Model {
    public partial class LimitInfo
    {
        public static List<LimitInfo> UserGetLimitInfos(string numberID, string enID)
        {
            LimitInfo lInfo = new LimitInfo() { NumberID = numberID, EnID = enID };
            return LimitInfo.SelLimitInfo(lInfo);
        }
    }
}

namespace Forget.HRBLL.View
{
    public partial class LimitView 
    {
        private static readonly string SELLIMITVIEW = "HR_View_SelUserLimitView";
        private static readonly string MODIFYUSERLIMITVIEW = "HR_View_ModifyUserLimitView";
        public static List<LimitView> GetUserLimits(LimitView view)
        {
            List<SqlParameter> parms = Convert<SqlParameter>.ObjectToParms(view);
            List<LimitView> views = null;
            try
            {
                DataTable table = DataAccess.ExecuteDataTable(CommandType.StoredProcedure, SELLIMITVIEW, parms.ToArray());
                views = Convert<LimitView>.DataConvertToObject(table);                
            }
            catch (Exception ex)
            {
                Log.WriteLog("BU.LimitViewSelUser", ex.Message, ex.StackTrace);
            }
            return views;
        }

        public static bool ModifyUserLimit(List<LimitView> views)
        {
            SqlTransaction trans = DataAccess.GetTransaction();
            try
            {
                foreach (LimitView v in views)
                {
                    List<SqlParameter> parms = Convert<SqlParameter>.ObjectToParms(v);
                    DataAccess.ExecuteNonQuery(trans, CommandType.StoredProcedure, MODIFYUSERLIMITVIEW, parms.ToArray());
                }
                trans.Commit();
            }
            catch (Exception ex)
            {
                trans.Rollback();                
                Log.WriteLog("BU.ModifyUserLimit", ex.Message, ex.StackTrace);
                return false;
            }
            return true;
        }        
    }
}